import dao.BlogDao;
import dao.BossDao;
import dao.StudentDao;
import domain.Blog;
import domain.Boss;
import domain.Student;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import util.MybatisUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Group2 {
    @Test
    public void test(){
        try(SqlSession sqlSession= MybatisUtil.getSqlSession()){
            StudentDao mapper = sqlSession.getMapper(StudentDao.class);
            List<Student> students = mapper.getStudents();
            for(Student student:students){
                System.out.println(student);
            }
        }
    }

    @Test
    public void test1(){
        try(SqlSession sqlSession= MybatisUtil.getSqlSession()){
            StudentDao mapper = sqlSession.getMapper(StudentDao.class);
            List<Student> students = mapper.getStudents2();
            for(Student student:students){
                System.out.println(student);
            }
        }
    }

    @Test
    public void test2(){
        try(SqlSession sqlSession= MybatisUtil.getSqlSession()){
            BossDao mapper = sqlSession.getMapper(BossDao.class);
            List<Boss> boss = mapper.getBoss();
            for(Boss b:boss){
                System.out.println(b);
            }
        }
    }

    @Test
    public void test3(){
        try(SqlSession sqlSession= MybatisUtil.getSqlSession()){
            BossDao mapper = sqlSession.getMapper(BossDao.class);
            List<Boss> boss = mapper.getBoss2();
            for(Boss b:boss){
                System.out.println(b);
            }
        }
    }

    @Test
    public void test4() {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()) {
            BlogDao mapper = sqlSession.getMapper(BlogDao.class);
            Blog blog = new Blog();
            blog.setAuthor("qianqian");
            blog.setTitle("hahahah");
            blog.setViews(200);
            blog.setCreateTime("2020-02-04 12:38:46");
            int affectRows = mapper.addBlog(blog);
            sqlSession.commit();
            assert affectRows > 0;
        }
    }

    @Test
    public void test5() {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()) {
            BlogDao mapper = sqlSession.getMapper(BlogDao.class);
            Map<String, Object> map = new HashMap<>();
            map.put("author","qianqianjun");
//            List<Blog> blogs = mapper.queryBlogIF2(map);
            List<Blog> blogs = mapper.queryBlogIF(map);
            for(Blog blog:blogs){
                System.out.println(blog);
            }
        }
    }

    @Test
    public void test6() {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()) {
            BlogDao mapper = sqlSession.getMapper(BlogDao.class);
            Map<String, Object> map = new HashMap<>();
//            map.put("author","qianqianjun");
            map.put("author","qianqian");
//            map.put("views",200);
            List<Blog> blogs = mapper.queryBlogByChoose(map);
            for(Blog blog:blogs){
                System.out.println(blog);
            }
        }
    }

    @Test
    public void test7() {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()) {
            BlogDao mapper = sqlSession.getMapper(BlogDao.class);
            Map<String, Object> map = new HashMap<>();
            map.put("id",1);
            map.put("title", "new title");
            map.put("author","qianqian");
            int affectRows = mapper.updateBlog(map);
            sqlSession.commit();
            assert affectRows>0;
        }
    }

    @Test
    public void test8() {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()) {
            BlogDao mapper = sqlSession.getMapper(BlogDao.class);
            Map<String, Object> map = new HashMap<>();
            ArrayList<Integer> ids=new ArrayList<>();
            ids.add(1);ids.add(2);
            map.put("ids", ids);
            List<Blog> blogs = mapper.queryBlogForeach(map);
            for (Blog blog : blogs) {
                System.out.println(blog);
            }
        }
    }
}
